package owl2vowl.parser.vowl.classes;

import java.util.Iterator;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.semanticweb.owlapi.model.OWLClass;
import org.semanticweb.owlapi.model.OWLDataPropertyExpression;
import org.semanticweb.owlapi.model.OWLHasKeyAxiom;
import org.semanticweb.owlapi.model.OWLObjectPropertyExpression;
import org.semanticweb.owlapi.model.OWLOntology;

import owl2vowl.constants.VowlAttribute;
import owl2vowl.model.data.VowlData;
import owl2vowl.model.entities.nodes.classes.AbstractClass;
import owl2vowl.model.entities.properties.VowlDatatypeProperty;
import owl2vowl.model.entities.properties.VowlObjectProperty;

public class HasKeyAxiomParser {
	private static final Logger logger = LogManager.getLogger(HasKeyAxiomParser.class);

	public static void parse(OWLOntology ontology, OWLClass owlClass, VowlData vowlData) {
		Iterator arg2 = ontology.getHasKeyAxioms(owlClass).iterator();

		while (arg2.hasNext()) {
			OWLHasKeyAxiom owlHasKeyAxiom = (OWLHasKeyAxiom) arg2.next();
			AbstractClass hasKeysClass = vowlData.getClassForIri(owlClass.getIRI());
			Iterator arg5 = owlHasKeyAxiom.getObjectPropertyExpressions().iterator();

			while (arg5.hasNext()) {
				OWLObjectPropertyExpression propertyExpression = (OWLObjectPropertyExpression) arg5.next();
				if (propertyExpression.isAnonymous()) {
					logger.info("Anonymous has key object property expression " + propertyExpression);
				} else {
					VowlObjectProperty property = vowlData
							.getObjectPropertyForIri(propertyExpression.asOWLObjectProperty().getIRI());
					property.addAttribute(VowlAttribute.KEY);
					hasKeysClass.addKey(property.getIri());
				}
			}

			arg5 = owlHasKeyAxiom.getDataPropertyExpressions().iterator();

			while (arg5.hasNext()) {
				OWLDataPropertyExpression propertyExpression1 = (OWLDataPropertyExpression) arg5.next();
				if (propertyExpression1.isAnonymous()) {
					logger.info("Anonymous has key data property expression " + propertyExpression1);
				} else {
					VowlDatatypeProperty property1 = vowlData
							.getDatatypePropertyForIri(propertyExpression1.asOWLDataProperty().getIRI());
					property1.addAttribute(VowlAttribute.KEY);
					hasKeysClass.addKey(property1.getIri());
				}
			}
		}

	}
}